﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using MyVideoViewerLibrary;

using Microsoft.MediaCenter.UI;

namespace MyVideoViewerMCPlugin
{
    public class MyMovie
    {
        public Movie MovieTitle { get; set; }
        private BooleanChoice _checkBoxItem = new BooleanChoice();

        public string Title { get { return MovieTitle.Title; } }
        public string SortTitle { get { return MovieTitle.SortTitle; } }
        public string Runtime
        {
            get
            {
                if (MovieTitle.Runtime > 0)
                {
                    TimeSpan span = TimeSpan.FromMinutes(MovieTitle.Runtime);
                    return string.Format("{0}h {1}m", span.Hours, span.Minutes);
                }
                else
                    return "";
            }
        }
        public string Studio
        {
            get
            {
                if (MovieTitle.Studios == null)
                    return "";
                else
                    return string.Join(", ", MovieTitle.Studios.Select(s => s.Name).ToArray());
            }
        }
        public string Rating 
        { 
            get 
            {
                return MovieTitle.MPAARating;
            } 
        }
        public string MovieDescription { get { return MovieTitle.Description; } }
        public string Genre
        {
            get
            {
                if (MovieTitle.Genres == null)
                    return "";
                else
                    return string.Join(", ", MovieTitle.Genres.Select(s => s.GenreDesc).ToArray());
            }
        }
        public string FolderImage 
        {
            get { return MovieTitle.PosterImageLocation; }
        }
        public bool ShowBackgroundImage
        {
            get
            {
                return File.Exists(MovieTitle.BackgroundImageLocation);
            }
        }
        public Image BackgroundImage
        {
            get
            {
                if (File.Exists(MovieTitle.BackgroundImageLocation))
                    return new Image("file://" + MovieTitle.BackgroundImageLocation);
                else
                    return new Image("resx://MyVideoViewerMCPlugin/MyVideoViewerMCPlugin.Resources/clear");
            }
        }
        public DateTime? ReleaseDate { get { return MovieTitle.ReleaseDate; } }
        public string RatingDescription
        {
            get
            {
                return MovieTitle.MPAARatingDescription;
            }
        }
        public string Director { get { return MovieTitle.Director; } }
        public bool ShowWatched { get { return MoviesBusiness.MoviesBL.SupportsWatched(); } }

        public Image Image
        {
            get
            {
                if (File.Exists(MovieTitle.PosterImageLocation))
                    return new Image("file://" + MovieTitle.PosterImageLocation);
                else
                    return new Image("resx://MyVideoViewerMCPlugin/MyVideoViewerMCPlugin.Resources/NoImage");
            }
        }

        public List<MediaFile> Files { get; set; }

        public BooleanChoice CheckBoxItem { get; set; }
        public BooleanChoice WatchTrailersCheckBoxItem { get; set; }
        public Choice SpinnerItems { get; set; }

        public int NumberOfTrailersToPlay { get; set; }


        void CheckBoxItem_PropertyChanged(IPropertyObject sender, string property)
        {
            if (property == "Value")
            {
                MovieTitle.Watched = CheckBoxItem.Value;
            }
        }

        public MyMovie()             
        {
            CheckBoxItem = new BooleanChoice();
            WatchTrailersCheckBoxItem = new BooleanChoice();

            CheckBoxItem.Description = "Watched";

            WatchTrailersCheckBoxItem.Description = "Watch Previews";
            WatchTrailersCheckBoxItem.Value = false;


            SpinnerItems = new Choice();
            List<string> stringItems = new List<string>() { "0", "1", "2", "3", "4", "5", "6" };
            SpinnerItems.Options = stringItems;
            SpinnerItems.ChosenChanged += new EventHandler(SpinnerItems_ChosenChanged);
        }

        public MyMovie(Movie title)
            : this()
        {
            MovieTitle = title;
            
            CheckBoxItem.Value = MovieTitle.Watched;
            CheckBoxItem.PropertyChanged += new PropertyChangedEventHandler(CheckBoxItem_PropertyChanged);          
        }

        void SpinnerItems_ChosenChanged(object sender, EventArgs e)
        {
            NumberOfTrailersToPlay = int.Parse(SpinnerItems.Chosen.ToString());
        }
               

        public void GetFiles()
        {
            MovieTitle.GetFiles();
            Files = new List<MediaFile>();
            foreach (string file in MovieTitle.Files.OrderBy(f=>Path.GetFileNameWithoutExtension(f)))
            {
                Files.Add(new MediaFile() { Path = file });
            }

            if (Files.Count == 1)
                Files[0].FileName = "Play";
            else
            {
                for (int i = 0; i < Files.Count; i++)
                {
                    Files[i].FileName = string.Format("Part {0}", i + 1);
                }
            }

            if (File.Exists(MovieTitle.Trailer))
            {
                Files.Add(new MediaFile() { FileName = "Trailer", Path = MovieTitle.Trailer });
            }
        }
    }
}
